﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Ccit.Utils;
using Ccit.OA.Utils;
using Ccit.Global;

namespace Ccit.OA.NewsCenter
{
    public class QuerySQL : IQuery
    {
        #region  
        
        /// <summary>
        /// 查询本人的快速查询条件表的信息
        /// </summary>
        /// <param name="loginUser"></param>
        /// <returns></returns>
        public List<MsQueryClause> GetFastClauseList(string loginUser,string Type)
        {
            string sType = (Type != "") ? Type : "0";
            string cmdText = string.Empty;
            if (string.IsNullOrEmpty(loginUser)) return null;

            cmdText = "SELECT NEWS_A8_10 AS CLAUSEID,NEWS_A8_40 AS  CLAUSENAME,NEWS_A8_30 AS CLAUSETYPE,NEWS_A8_80 AS FUNCTYPE FROM NEWS_A8 WHERE NEWS_A8_9994='" + loginUser + "' AND NEWS_A8_60='1' AND NEWS_A8_30 = '" + sType + "'";
            //if (PlateIds != "")
            //    cmdText += "AND NEWS_A8_10 IN (" + PlateIds + ")";
            //else
            //    return null;
            List<MsQueryClause> msclist = new List<MsQueryClause>();
            using(IDataReader ireader =DataAccessor.ExecuteReader(cmdText))
            {
                while(ireader.Read()){
                    MsQueryClause msc = new MsQueryClause();
                    msc.ClauseId = ireader["CLAUSEID"].ToString();
                    msc.ClauseName = ireader["CLAUSENAME"].ToString();
                    msc.ClauseType = ireader["CLAUSETYPE"].ToString();
                    msc.FuncType = ireader["FUNCTYPE"].ToString();
                    msclist.Add(msc);
                }
            }
            return msclist;
        }
        /// <summary>
        /// delete fast clause
        /// </summary>
        /// <param name="ClauseId"></param>
        /// <returns></returns>
        public List<string> RemoveFastClause(string ClauseId)
        {
            List<string> strlist = new List<string>();
            if(string.IsNullOrEmpty(ClauseId))
            {
                strlist.Add("-999");
                strlist.Add("删除失败,缺少必要的参数");
                return strlist;
            }
            string cmdText = " DELETE FROM NEWS_A8 WHERE NEWS_A8_10='"+ClauseId+"'";
            if (0 < DataAccessor.ExecuteNonQuery(cmdText))
            {
                strlist.Add("1");
                strlist.Add("删除成功");
            }
            else
            {
                strlist.Add("-1");
                strlist.Add("删除失败,系统忙,请稍候再试");
            }
            string cmdPlate = "DELETE FROM NEWS_B2 WHERE NEWS_B2_10='"+ClauseId+"'";
            if (0 < DataAccessor.ExecuteNonQuery(cmdPlate))
            {
                strlist.Add("1");
                strlist.Add("删除成功");
            }
            else
            {
                strlist.Add("-1");
                strlist.Add("删除失败,系统忙,请稍候再试");
            }
            return strlist;
        }
        /// <summary>
        /// save fast clause
        /// </summary>
        /// <param name="msc"></param>
        /// <param name="addPlate"></param>
        /// <returns></returns>
        public List<string> SaveFastClause(MsQueryClause msc, string addPlate, string Type, string pageSource)
        {
            if (msc == null) return null;
            List<string> strlist = new List<string>();
            if (msc.ClauseId == "")
            {
                strlist = doSave(msc, addPlate, Type, pageSource);
            }
            else
            {
                strlist = doEdit(msc,addPlate,Type);
            }
            return strlist;
            
        }
        //新增
        private static List<string> doSave(MsQueryClause msc, string addPlate, string Type, string pageSource)
        {
            string sType = (Type != "") ? Type : "0";
            List<string> strlist = new List<string>();
            string[] pubMsg = SessionExt.GetSysFieldSQL("NEWS_A8");
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            string strMaxId = DataInterface.GetMaxId("NEWS_A8", "NEWS_A8_10").ToString();
            //快速查询条件表
            string winFlag = string.Empty;
            if (addPlate == "1")
                winFlag = "1";
            else
                winFlag = "0";

            string strFastClause = "INSERT INTO NEWS_A8 (NEWS_A8_10,NEWS_A8_20,NEWS_A8_30,NEWS_A8_40,NEWS_A8_50,NEWS_A8_60,NEWS_A8_70,NEWS_A8_80 ," + pubMsg[0] + ") " +
                            " VALUES ('" + strMaxId + "','" + loginUser.UserId + "','" + sType + "','" + msc.ClauseName + "','" + msc.ClauseContent + "','1','" + winFlag + "','"+pageSource+"'," + pubMsg[1] + ")";
            if (0 < DataAccessor.ExecuteNonQuery(strFastClause))
            {
                strlist.Add("1");
                strlist.Add("保存成功");
                strlist.Add(strMaxId);
            }
            else
            {
                strlist.Add("-1");
                strlist.Add("保存失败,系统忙,请稍后再试");
                strlist.Add("");
            }
            if (addPlate == "1")  //选中复选框
            {
                //版块表
                string strPlate = "INSERT INTO NEWS_B2 (NEWS_B2_10,NEWS_B2_20,NEWS_B2_40,NEWS_B2_70,NEWS_B2_80,NEWS_B2_90,NEWS_B2_100,NEWS_B2_120,NEWS_B2_130)";
                strPlate += "VALUES ('" + strMaxId + "','" + msc.ClauseName + "','0','0','0','1','1','"+sType+"','"+loginUser.UserId+"')";
                if (0 < DataAccessor.ExecuteNonQuery(strPlate))
                {
                    strlist.Add("1");
                    strlist.Add("保存成功");
                    strlist.Add(strMaxId);
                }
                else
                {
                    strlist.Add("-1");
                    strlist.Add("保存失败,系统忙,请稍后再试");
                    strlist.Add("");
                }
            }
            return strlist;
        }
        //编辑
        private static List<string> doEdit(MsQueryClause msc, string addPlate,string Type)
        {
            List<string> strlist = new List<string>();
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            string cmdText = "UPDATE NEWS_A8 SET NEWS_A8_40 = '" + msc.ClauseName + "' , NEWS_A8_50='" + msc.ClauseContent + "' WHERE NEWS_A8_10 = '"+msc.ClauseId+"' ";
            if (0 < DataAccessor.ExecuteNonQuery(cmdText))
            {
                strlist.Add("1");
                strlist.Add("保存成功");
                strlist.Add(msc.ClauseId);
            }
            else
            {
                strlist.Add("-1");
                strlist.Add("保存失败");
                strlist.Add("");
            }
            if (addPlate == "1")
            {  //选中复选框
                string selPlate = "SELECT COUNT(*) FROM NEWS_B2 WHERE NEWS_B2_10 = '" + msc.ClauseId + "'";
                if (0 == Convert.ToInt32(DataAccessor.ExecuteScalar(selPlate)))
                {
                    string sType = (Type != "") ? Type : "0";
                    //版块表
                    string strPlate = "INSERT INTO NEWS_B2 (NEWS_B2_10,NEWS_B2_20,NEWS_B2_40,NEWS_B2_70,NEWS_B2_80,NEWS_B2_90,NEWS_B2_100,NEWS_B2_120,NEWS_B2_130)";
                    strPlate += "VALUES ('" + msc.ClauseId + "','" + msc.ClauseName + "','0','0','0','1','1','" + sType + "','"+login.UserId+"')";
                    if (0 < DataAccessor.ExecuteNonQuery(strPlate))
                    {
                        strlist.Add("1");
                        strlist.Add("保存成功");
                        strlist.Add(msc.ClauseId);
                    }
                    else
                    {
                        strlist.Add("-1");
                        strlist.Add("保存失败,系统忙,请稍后再试");
                        strlist.Add("");
                    }
                }

                string upFastClause = "UPDATE NEWS_A8 SET NEWS_A8_70 = '1' WHERE NEWS_A8_10 ='" + msc.ClauseId + "' ";
                int iexe = DataAccessor.ExecuteNonQuery(upFastClause);
            }
            else
            {
                string delPlate = "DELETE FROM NEWS_B2 WHERE NEWS_B2_10 = '"+msc.ClauseId+"'";
                if (0 < DataAccessor.ExecuteNonQuery(delPlate))
                {
                    strlist.Add("1");
                    strlist.Add("保存成功");
                    strlist.Add(msc.ClauseId);
                }
                else
                {
                    strlist.Add("-1");
                    strlist.Add("保存失败,系统忙,请稍后再试");
                    strlist.Add("");
                }
                string upFastClause = "UPDATE NEWS_A8 SET NEWS_A8_70 = '0' WHERE NEWS_A8_10 ='" + msc.ClauseId + "' ";
                int iexe = DataAccessor.ExecuteNonQuery(upFastClause);
            }
            return strlist;
        }
        /// <summary>
        /// 查询快捷条件对象
        /// </summary>
        /// <param name="clauseId">主键NEWS_A8_10</param>
        /// <returns></returns>
        public MsQueryClause GetClauseObj(string clauseId)
        {
            MsQueryClause msq = null;
            if (clauseId=="") return msq;
            string cmdText = "SELECT NEWS_A8_10,NEWS_A8_40,NEWS_A8_50 FROM NEWS_A8 WHERE NEWS_A8_10=" + clauseId;
            using(IDataReader ireader=DataAccessor.ExecuteReader(cmdText))
            {
                while(ireader.Read()){
                    msq = new MsQueryClause();
                    msq.ClauseId = ireader["NEWS_A8_10"].ToString();
                    msq.ClauseName = ireader["NEWS_A8_40"].ToString();
                    msq.ClauseContent = ireader["NEWS_A8_50"].ToString();
                }
            }
            return msq;
        }
        #endregion

    }
}
